<template>
  <div>

    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户</el-breadcrumb-item>
      <el-breadcrumb-item>用户账号</el-breadcrumb-item>
    </el-breadcrumb>

    <el-form :inline="true" :model="query" class="query-form" label-width="4rem">
      <el-form-item label="用户名">
        <el-input v-model="query.username" placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item label="账  号">
        <el-input v-model="query.account" placeholder="请输入账号"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onQuery">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onAdd">新增</el-button>
      </el-form-item>
    </el-form>

    <el-table class="table" :data="getUsers" border stripe>
      <el-table-column prop="username" label="用户名">
      </el-table-column>
      <el-table-column prop="account" label="账号">
      </el-table-column>
      <el-table-column prop="phonenum" label="联系方式">
      </el-table-column>
      <el-table-column prop="email" label="邮箱">
      </el-table-column>
      <el-table-column label="操作" min-width="120">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          <!-- <el-button size="mini" type="danger">禁用</el-button> -->
          <!-- <el-button size="mini" type="success">启用</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <!-- <el-pagination class="pagination" background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="1" :page-sizes="[10, 20, 30, 40]" :page-size="10" layout="total, sizes, prev, pager, next, jumper" :total="400">
    </el-pagination> -->

  </div>
</template>

<style>
.query-form {
  margin-top: 20px;
  padding-top: 25px;
  background-color: #f2f2f2;
}

.table {
  margin-top: 30px;
}

.pagination {
  margin-top: 30px;
  text-align: right;
  padding-right: 2rem;
}
</style>

<script>
import { mapGetters, mapActions } from "vuex";

export default {
  name: "Users",
  created() {
    console.log("hmx^^^^^^^|created");
    this.reqGetUser();
  },
  computed: {
    ...mapGetters(["getUsers"])
  },
  data() {
    return {
      query: {
        username: "",
        account: ""
      }
    };
  },
  methods: {
    ...mapActions(["reqGetUser", "reqDeleteUser"]),
    onQuery() {
      console.log("onQuery!");
    },
    onAdd() {
      console.log("onAdd!");
      this.$router.push({ name: "AddUser", params: {} });
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
    },
    handleEdit(index, row) {
      console.log(index);
      console.log(row);
      this.$router.push({
        name: "EditUser",
        params: {
          _id: row._id,
          account: row.account,
          username: row.username,
          phonenum: row.phonenum,
          email: row.email
        }
      });
    },
    handleDelete(index, row) {
      this.$confirm("此操作将永久删除该用户, 是否继续?", "提示", {
        confirmButtonText: "确定",
        confirmButtonClass: "el-button--danger",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.reqDeleteUser({ _id: row._id });
        })
        .catch(() => {});
    }
  }
};
</script>
